package com.iwunu.gateway.util;

public class CreateOfflinePasswordUtil {
	 private static  char  ciphertext[]={7,2,6,4,9,0,3,1,8,5}; 
     private static  char   ciphertextAdd[]={5,9,6,4,7,9,4,9};
     private static  char  ciphertextOrx[]={2,7,6,4,3,3,4,1};	
     private static  char  variationFactor[] ={ 
  		       1,4,6,4,8,6,3,5,       // 0
				   5,4,3,7,9,4,4,7,
				   2,3,3,8,7,3,1,1,
				   8,1,3,7,3,5,8,6,
				   6,4,3,4,8,9,4,4,
				   3,4,1,4,6,7,3,2,
				   9,7,5,5,2,2,2,3,
				   4,9,4,8,7,3,4,7,
				   4,3,4,7,6,4,1,9,
				   3,7,9,7,6,4,6,4,

				   8,7,6,4,2,1,4,7,       // 1
				   0,6,3,7,6,3,3,2,
				   9,5,5,3,7,0,6,3,
				   3,1,5,9,6,4,3,9,
				   5,7,6,5,1,1,7,7,
				   1,6,5,2,2,3,4,1,
				   4,7,4,1,1,2,1,3,
				   7,3,7,9,1,2,5,4,
				   5,8,8,5,4,1,1,0,
				   6,1,4,9,4,4,1,6,

				    2,3,9,4,6,1,1,3,      // 2
				    4,1,9,1,1,5,1,4,
				    4,5,6,1,3,7,8,3,
				    2,4,7,9,1,3,6,9,
				    9,5,4,3,4,7,5,1,
				    7,1,2,3,6,7,5,4,
				    8,9,5,3,1,5,7,6,
				    6,8,7,1,5,4,6,5,
				    1,1,5,3,5,4,8,7,
				    5,8,7,6,4,1,2,


				    8,4,3,8,4,6,4,8,       //  3
				    5,5,4,6,4,1,2,6,
				    8,4,3,4,8,4,6,9,
				    4,3,1,5,4,7,9,1,
				    4,6,7,2,2,5,4,3,
				    3,2,4,5,3,4,0,2,
				    9,0,5,6,4,8,1,4,
				    3,5,4,1,5,7,2,2,
				    1,5,0,4,0,6,4,1,
				    6,2,3,4,1,6,5,6,

				    6,8,4,3,1,5,4,7,       // 4
				    3,2,5,9,7,4,6,6,
				    1,6,5,5,8,9,7,7,
				    4,9,8,4,3,1,4,6,
				    6,4,1,4,7,4,3,1,
				    4,6,7,4,6,4,1,3,
				    5,7,8,9,4,3,0,8,
				    1,3,4,7,2,8,7,8,
				    8,6,1,3,2,1,0,4,
				    5,8,7,8,7,4,0,1, 

				    8,7,3,1,5,4,5,7,       // 5
				    0,5,7,4,1,2,0,1,
				    0,8,7,4,0,2,1,6,
				    6,4,7,3,7,4,6,1,
				    3,2,0,9,8,4,7,3,
				    4,4,8,7,8,5,4,1,
				    1,1,2,1,9,0,1,6,
				    6,5,7,8,9,7,4,1,
				    5,3,3,0,8,3,2,3,
				    7,5,0,2,7,3,8,9,

				   4,6,4,8,7,6,4,3,         //6
				   8,7,8,0,4,0,7,4,
				   8,0,4,6,0,7,8,6,
				   7,6,4,9,8,7,6,4,
				   4,6,7,8,6,6,8,1,
				   8,4,8,8,2,2,8,7,
				   4,2,4,2,4,6,8,5,
				   4,8,1,2,1,1,6,6,
				   8,8,8,0,1,8,7,3,
				   9,8,0,7,8,0,7,4,

				   7,8,1,3,4,4,3,5,
				   7,5,1,6,1,3,4,9,
				   7,1,7,0,8,7,8,9,
				   7,4,0,4,8,7,9,1,
				   0,6,9,7,1,3,0,4,
				   8,2,4,7,4,5,4,8,
				   9,7,5,7,0,8,7,7,
				   4,7,9,4,5,4,8,7,
				   4,7,4,5,4,8,7,4,
				   5,0,8,5,6,7,7,4,
				   //----------------------------
				   1,4,5,6,4,6,3,1,2,1,4,8,7,8,4
				};
	
	public static String Encryption(String time,String baseCode,int flag){
		StringBuffer sb = new StringBuffer();
		int i,j =0;
		char[] Buf = new char[8];
//		  tm.year-=18; 
		  Buf[0]=(char) (getDataTime(time,"")/6%10);//分
		  Buf[1]=(char) ((getDataTime(time,"y")-18)%10);//年
		  Buf[2]=(char) (getDataTime(time,"m")%10);//月
	      Buf[3]=(char) (getDataTime(time,"d")%10);//日的地位
	      Buf[4]=(char) (((getDataTime(time,"d")/10)<<1));//日的高位|标志位
	      Buf[5]=(char) (getDataTime(time,"h")%10);//时间         
	      Buf[6]=(char) (((getDataTime(time,"m")/10)<<2)|((getDataTime(time,"h")/10)<<0));//月的地位|时间的高位。
/*	      Buf[7]=4;
	      for(i=0;i<7;i++){
	          Buf[7]+=Buf[i];         
	      }
	       Buf[7]%=10;*/
	      Buf[7] = (char) flag;
	      for(i=0;i<8;i++) {
	         Buf[i]+=ciphertextAdd[i];
	         Buf[i]%=10;
	      }
	      for(i=0;i<8;i++){
	           Buf[i]=ciphertext[Buf[i]];
	      }

	      for(i=0;i<8;i++) {
	         Buf[i]=(char) ((int)Buf[i]+Integer.parseInt(baseCode.substring(i, i+1)));
	         Buf[i]%=10;
	      }      
	      
	      for(i=0;i<8;i++){         
	         if(Buf[i]<8)
	         {
	            Buf[i]^=(ciphertextOrx[i]&0x07);  
	         }
	      }
	     
	      for(i=0;i<8;i++) { 
	         for(j=0;j<8;j++){
	              if(i!=j)                 
	              {
	                  if(j%2==1)
	                  {
	                    Buf[j]+=variationFactor[(Buf[i]*8)+i*80+j];
	                    Buf[j]%=10;
	                  }
	                  else
	                  {
	                     if(Buf[j]<8)
	                     {
	                         Buf[j]^=(variationFactor[(Buf[i]*8)+i*80+j]&0x07);
	                     }   
	                  }
	               }   
	         }    
	      }
	      for(int k =0;k<Buf.length;k++){
	    	  sb.append((int)Buf[k]);
	      }
		return sb.toString() ;
	}
	/**
	 * 
	 * @param time
	 * @param str
	 * @return
	 */
	public static int getDataTime(String time ,String str){
		if("y".equals(str)){
			String year = time.substring(0,2);
			return Integer.parseInt(year);
		}else if("m".equals(str)){
			String moon = time.substring(2,4);
			return Integer.parseInt(moon);	
		}
		else if("d".equals(str)){
			String day = time.substring(4,6);
			return Integer.parseInt(day);	
		}
		else if("h".equals(str)){
			String hour = time.substring(6,8);
			return Integer.parseInt(hour);	
		}else{
			String minute = time.substring(8,10);
			return Integer.parseInt(minute);	
		}
	}


}
